﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;

namespace SPVSX.Explorer.Commands.SharePoint
{
    /// <summary>
    /// The Custom Action Group Element
    /// </summary>
    public class CustomActionGroupElement : IComparable<CustomActionGroupElement>
    {
        #region Const Fields

        /// <summary>
        /// Constant to hold the description xml attribute name
        /// </summary>
        internal const string attributeNameDescription = "Description";

        /// <summary>
        /// Constant to hold the id xml attribute name
        /// </summary>
        internal const string attributeNameId = "Id";

        /// <summary>
        /// Constant to hold the location xml attribute name
        /// </summary>
        internal const string attributeNameLocation = "Location";

        /// <summary>
        /// Constant to hold the sequence xml attribute name
        /// </summary>
        internal const string attributeNameSequence = "Sequence";

        /// <summary>
        /// Constant to hold the title xml attribute name
        /// </summary>
        internal const string attributeNameTitle = "Title";

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the internal <see cref="SPElementDefinition"/>
        /// </summary>
        private SPElementDefinition InternalElement { get; set; }

        /// <summary>
        /// Gets the longer description that is exposed as a sub-description for the action group.
        /// </summary>
        /// <remarks>Optional Text</remarks>
        public string Description
        {
            get
            {
                if (!String.IsNullOrEmpty(InternalElement.FeatureDefinition.DefaultResourceFile))
                {
                    return
                        Utility.GetLocalisedStringFromHiveResources(
                            Utility.XmlGetAttributeValue(InternalElement.XmlDefinition,
                                                         attributeNameDescription),
                            InternalElement.FeatureDefinition.DefaultResourceFile);
                }
                return
                    Utility.GetLocalisedStringFromHiveResources(
                        Utility.XmlGetAttributeValue(InternalElement.XmlDefinition,
                                                     attributeNameDescription));
            }
        }

        /// <summary>
        /// Gets the unique identifier for the element. The ID may be a GUID, or it may be a unique term
        /// </summary>
        /// <remarks>Optional Text</remarks>
        /// <example>"SiteManagement". See Default Custom Action Locations and IDs for a list of the default custom action group IDs that are used in Windows SharePoint Services.</example>
        public string Id
        {
            get { return Utility.XmlGetAttributeValue(InternalElement.XmlDefinition, attributeNameId); }
        }

        /// <summary>
        /// Gets the value for where the action lives.
        /// </summary>
        /// <remarks>Required Text</remarks>
        public string Location
        {
            get { return Utility.XmlGetAttributeValue(InternalElement.XmlDefinition, attributeNameLocation); }
        }

        /// <summary>
        /// Gets the Element tag
        /// </summary>
        public static string RootTag
        {
            get { return "CustomActionGroup"; }
        }

        /// <summary>
        /// Gets the ordering priority for the action group.
        /// </summary>
        /// <remarks>Optional Integer</remarks>
        public int Sequence
        {
            get
            {
                return Utility.XmlGetAttributeValueAsInt32(InternalElement.XmlDefinition, attributeNameSequence,
                                                           0x7fffffff);
            }
        }

        /// <summary>
        /// Gets the end user description for the action group.
        /// </summary>
        /// <remarks>Required Text</remarks>
        public string Title
        {
            get
            {
                if (!String.IsNullOrEmpty(InternalElement.FeatureDefinition.DefaultResourceFile))
                {
                    return
                        Utility.GetLocalisedStringFromHiveResources(
                            Utility.XmlGetAttributeValue(InternalElement.XmlDefinition,
                                                         attributeNameTitle),
                            InternalElement.FeatureDefinition.DefaultResourceFile);
                }
                return
                    Utility.GetLocalisedStringFromHiveResources(
                        Utility.XmlGetAttributeValue(InternalElement.XmlDefinition,
                                                     attributeNameTitle));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Create a new CustomActionGroupElement object
        /// </summary>
        /// <param name="element">The <see cref="SPElementDefinition"/> object</param>
        public CustomActionGroupElement(SPElementDefinition element)
        {
            InternalElement = element;
        }

        /// <summary>
        /// Compare which uses the Sequence property
        /// </summary>
        /// <param name="caOtherGroup">The <see cref="CustomActionGroupElement"/> under comparison</param>
        /// <returns>The result of the comparison</returns>
        int IComparable<CustomActionGroupElement>.CompareTo(CustomActionGroupElement caOtherGroup)
        {
            int sequence = caOtherGroup.Sequence;
            return (Sequence - sequence);
        }

        #endregion
    }
}